import 'package:flutter/material.dart';
import 'package:example/base/custom_dialog.dart';
import 'package:example/config/color_config.dart';
import 'package:example/utils/dialog_ext.dart';
import 'package:example/widget/primary_btn.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../utils/common.dart';


/// 通用确认弹框-两个按钮
class NewConfirmDialog extends CustomDialog<NewConfirmDialogController> {
  NewConfirmDialog({
    NewConfirmDialogController? controller,
    String? title,
    String? content,
    Color? contentTextColor = ColorConfig.text,
    String? confirmText ,
    Color? confirmTextColor = ColorConfig.FF007AFF,
    String? cancelText,
    Color? cancelTextColor = Colors.black,
    bool? hideCancel,
    Widget Function(NewConfirmDialogController controller)? contentBuilder,
    Function(NewConfirmDialog dialog)? confirm,
    Function(NewConfirmDialog dialog)? cancel,
  }) : super(
            NewConfirmDialogController(
              title: title,
              content: content,
              confirmText: confirmText,
              confirmTextColor: confirmTextColor,
              cancelText: cancelText,
              cancelTextColor: cancelTextColor,
              contentBuilder: contentBuilder,
              hideCancel: hideCancel,
              confirm: confirm,
              cancel: cancel,
              contentTextColor: contentTextColor
            ),
            externalController: controller) {
    if (controller != null) {
      controller.title = controller.title ?? title;
      controller.content = controller.content ?? content;
      controller.confirmText = controller.confirmText ?? confirmText;
      controller.confirmTextColor =
          controller.confirmTextColor ?? confirmTextColor;
      controller.cancelText = controller.cancelText ?? cancelText;
      controller.cancelTextColor = controller.cancelTextColor ?? cancelTextColor;
      controller.hideCancel = controller.hideCancel ?? hideCancel;
      controller.confirm = controller.confirm ?? confirm;
      controller.cancel = controller.cancel ?? cancel;
    }
  }

  @override
  Widget initView(BuildContext context, NewConfirmDialogController controller) {
    return Center(
      child: Container(
        width: 287.w,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(12.r),
        ),
        child: _body(context, controller),
      ),
    );
  }

  _body(BuildContext context, NewConfirmDialogController controller) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        _title(controller),
        controller.contentBuilder == null
            ? _content(controller)
            : controller.contentBuilder?.call(controller),
        _bottomLayout(controller),
      ],
    );
  }

  _title(NewConfirmDialogController controller) {
    return Visibility(
      visible: isNotEmptyOrNull(controller.title),
      child: Container(
        padding: const EdgeInsets.only(top: 22),
        child: Text(
          controller.title ?? "",
          textAlign: TextAlign.center,
          style: TextStyle(
            color: Colors.black,
            fontSize: 18.sp,
            fontWeight: FontWeight.w500,
          ),
        ),
      ),
    );
  }

  _content(NewConfirmDialogController controller) {
    return Container(
      padding: EdgeInsets.fromLTRB(
          14.w, isNotEmptyOrNull(controller.title) ? 18.w : 35.w, 14.w, 32.w),
      alignment: Alignment.center,
      child: Text(
        controller.content ?? "",
        maxLines: 4,
        overflow: TextOverflow.ellipsis,
        textAlign: TextAlign.center,
        style: TextStyle(
          color: controller.contentTextColor,
          fontSize: 16.sp,
          fontWeight: FontWeight.w500,
        ),
      ),
    );
  }

  _bottomLayout(NewConfirmDialogController controller) {
    return Column(
      children: [
        Container(
          color: ColorConfig.FFD8D8D8,
          width: double.infinity,
          height: 0.5.w,
        ),
        Row(
          children: [
            Visibility(
              visible: !(controller.hideCancel ?? false),
              child: Expanded(
                child: PrimaryBtn(
                  height: 48.w,
                  borderRadius:
                      BorderRadius.only(bottomLeft: Radius.circular(12.r)),
                  text: controller.cancelText ?? "取消",
                  textColor: controller.cancelTextColor ?? Colors.black,
                  fontSize: 16.sp,
                  onTap: () {
                    if (controller.cancel == null) {
                      hide();
                    } else {
                      controller.cancel?.call(this);
                    }
                  },
                ),
              ),
            ),
            Visibility(
              visible: !(controller.hideCancel ?? false),
              child: Container(
                color: ColorConfig.FFD8D8D8,
                height: 48.w,
                width: 0.5.w,
              ),
            ),
            Expanded(
              child: PrimaryBtn(
                height: 48.w,
                borderRadius: !(controller.hideCancel ?? false)
                    ? BorderRadius.only(bottomRight: Radius.circular(12.r))
                    : BorderRadius.vertical(bottom: Radius.circular(12.r)),
                text: controller.confirmText ?? "确定",
                textColor: controller.confirmTextColor,
                fontSize: 16.sp,
                onTap: () {
                  if (controller.confirm == null) {
                    hide();
                  } else {
                    controller.confirm?.call(this);
                  }
                },
              ),
            ),
          ],
        ),
      ],
    );
  }
}

class NewConfirmDialogController extends GetxController {
  String? title;
  String? content;
  Color? contentTextColor;
  String? confirmText;
  Color? confirmTextColor;
  String? cancelText;
  Color? cancelTextColor;
  bool? hideCancel;
  Widget Function(NewConfirmDialogController controller)? contentBuilder;
  Function(NewConfirmDialog dialog)? confirm;
  Function(NewConfirmDialog dialog)? cancel;

  NewConfirmDialogController({
    this.title,
    this.content,
    this.confirmText,
    this.confirmTextColor,
    this.cancelText,
    this.cancelTextColor,
    this.hideCancel,
    this.contentBuilder,
    this.confirm,
    this.cancel,
    this.contentTextColor
  });
}
